﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ContactManager.Common;

namespace ContactManager
{
    public partial class IncMonthFrm : AlSkin.AlForm.AlBaseForm
    {
        private string year = "";

        private string month = "";

        public IncMonthFrm()
        {
            InitializeComponent();
        }

        public IncMonthFrm(string year)
        {
            this.year = year;
            InitializeComponent();
        }

        private void combRecent_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBoxItem recent = (ComboBoxItem)this.combRecent.SelectedItem;
            TracesForm tf = new TracesForm(recent.Value.ToString());
            tf.ShowDialog();
        }

        private void IncMonthFrm_Load(object sender, EventArgs e)
        {
            this.labelContactName.Text = Constant.TITLE + GlobleUser.USER_NAME;

            //初始化最近联系人
            this.combRecent.Items.Clear();
            string recentSql = "select top 18 c.c_id,c.c_name,max(t.t_time) from contacters c,traces t where c.c_id = t.t_c_id group by c.c_id,c.c_name order by max(t.t_time) desc";
            DataSet recentDs = AccessHelper.ExecuteQuery(recentSql);
            foreach (DataRow dr in recentDs.Tables[0].Rows)
            {
                ComboBoxItem ci = new ComboBoxItem();
                ci.Text = Convert.ToString(dr["c_name"]);
                ci.Value = dr["c_id"];
                this.combRecent.Items.Add(ci);
            }

            //初始化一些数据
            DateTime now = DateTime.Now;
            if (year.Length == 0)
            {
                year = now.ToString("yyyy");
            }
            this.labYear.Text = "统计年度:" + year;
            this.month = now.ToString("MM");
            
            DateTime startSum = new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), 1);
            DateTime endSum = startSum.AddMonths(1).AddDays(-1);
            string sql = " and i_date >=#" + startSum.ToString("yyyy/MM/dd") + "# and i_date <=#" + endSum.ToString("yyyy/MM/dd") + "#";
            getSum(sql);

            switch (month)
            { 
                case "01" :
                    this.labM1.ForeColor = Color.Red;
                    break;
                case "02":
                    this.labM2.ForeColor = Color.Red;
                    break;
                case "03":
                    this.labM3.ForeColor = Color.Red;
                    break;
                case "04":
                    this.labM4.ForeColor = Color.Red;
                    break;
                case "05":
                    this.labM5.ForeColor = Color.Red;
                    break;
                case "06":
                    this.labM6.ForeColor = Color.Red;
                    break;
                case "07":
                    this.labM7.ForeColor = Color.Red;
                    break;
                case "08":
                    this.labM8.ForeColor = Color.Red;
                    break;
                case "09":
                    this.labM9.ForeColor = Color.Red;
                    break;
                case "10":
                    this.labM10.ForeColor = Color.Red;
                    break;
                case "11":
                    this.labM11.ForeColor = Color.Red;
                    break;
                case "12":
                    this.labM12.ForeColor = Color.Red;
                    break;
                default :
                    MessageBox.Show("日期出错，请重试");
                    break;
            }


            

            //统计
            string dateThis = year + "/" + month;
            DateTime dtThis = Convert.ToDateTime(dateThis);
            DateTime dtNext = dtThis.AddMonths(1);
            string dateSql = "and t.i_date >=#" + dtThis.ToString("yyyy/MM/dd") + "# and t.i_date <=#" + dtNext.ToString("yyyy/MM/dd") + "#";

            initDataGridView(dateSql);
        }

        private void page(string month)
        {
            this.month = month;

            string dateThis = year + "/" + month;
            DateTime dtThis = Convert.ToDateTime(dateThis);
            DateTime dtNext = dtThis.AddMonths(1);
            string dateSql = "and t.i_date >=#" + dtThis.ToString("yyyy/MM/dd") + "# and t.i_date <=#" + dtNext.ToString("yyyy/MM/dd") + "#";
            initDataGridView(dateSql);
            getSum(dateSql);
            
            this.labM1.ForeColor = Color.Black;
            this.labM2.ForeColor = Color.Black;
            this.labM3.ForeColor = Color.Black;
            this.labM4.ForeColor = Color.Black;
            this.labM5.ForeColor = Color.Black;
            this.labM6.ForeColor = Color.Black;
            this.labM7.ForeColor = Color.Black;
            this.labM8.ForeColor = Color.Black;
            this.labM9.ForeColor = Color.Black;
            this.labM10.ForeColor = Color.Black;
            this.labM11.ForeColor = Color.Black;
            this.labM12.ForeColor = Color.Black;
        }

        private void initDataGridView(string sql)
        {
            this.dataGridViewIncMonth.Rows.Clear();
            //传入and条件。主要用于日期条件
            string totalSql = "select format(t.i_date,'yyyy/mm/dd') as i_date,p.t_name as name,sum(IIF(t.i_iorp=0,t.i_num_one,0)) as inc_one,sum(IIF(t.i_iorp=0,t.i_num_second,0)) as inc_second," +
                              "sum(IIF(t.i_iorp=1,t.i_num_one,0)) as pay_one,sum(IIF(t.i_iorp=1,t.i_num_one,0)) as pay_second " +
                              "from incpay t,typeinc p where t.i_type_id = p.t_id " + sql + " group by format(t.i_date,'yyyy/mm/dd'),p.t_name";
            DataSet totalDs = AccessHelper.ExecuteQuery(totalSql);
            foreach (DataRow dr in totalDs.Tables[0].Rows)
            {
                DateTime mdate =Convert.ToDateTime(dr["i_date"].ToString());
                string week = DateUtil.toChinaWeek(mdate);
                string tname = dr["name"].ToString();
                string inc_one = dr["inc_one"].ToString();
                string inc_second = dr["inc_second"].ToString();
                string pay_one = dr["pay_one"].ToString();
                string pay_second = dr["pay_second"].ToString();

                this.dataGridViewIncMonth.Rows.Add(mdate.ToString("yyyy/MM/dd"), week, tname, inc_one, inc_second, pay_one, pay_second);
            }
        }

        private void labM1_Click(object sender, EventArgs e)
        {
            page("1");
            this.labM1.ForeColor = Color.Red;
        }

        private void labM2_Click(object sender, EventArgs e)
        {
            page("2");
            this.labM2.ForeColor = Color.Red;
        }

        private void labM3_Click(object sender, EventArgs e)
        {
            page("3");
            this.labM3.ForeColor = Color.Red;
        }

        private void labM4_Click(object sender, EventArgs e)
        {
            page("4");
            this.labM4.ForeColor = Color.Red;
        }

        private void labM5_Click(object sender, EventArgs e)
        {
            page("5");
            this.labM5.ForeColor = Color.Red;
        }

        private void labM6_Click(object sender, EventArgs e)
        {
            page("6");
            this.labM6.ForeColor = Color.Red;
        }

        private void labM7_Click(object sender, EventArgs e)
        {
            page("7");
            this.labM7.ForeColor = Color.Red;
        }

        private void labM8_Click(object sender, EventArgs e)
        {
            page("8");
            this.labM8.ForeColor = Color.Red;
        }

        private void labM9_Click(object sender, EventArgs e)
        {
            page("9");
            this.labM9.ForeColor = Color.Red;
        }

        private void labM10_Click(object sender, EventArgs e)
        {
            page("10");
            this.labM10.ForeColor = Color.Red;
        }

        private void labM11_Click(object sender, EventArgs e)
        {
            page("11");
            this.labM11.ForeColor = Color.Red;
        }

        private void labM12_Click(object sender, EventArgs e)
        {
            page("12");
            this.labM12.ForeColor = Color.Red;
        }

        private void dataGridViewIncMonth_DoubleClick(object sender, EventArgs e)
        {
            if (this.dataGridViewIncMonth.CurrentRow != null)
            {
                DateTime m_date = Convert.ToDateTime(this.dataGridViewIncMonth.CurrentRow.Cells["m_date"].Value.ToString());
                string sql = " and i_date >=#" + m_date.ToString("yyyy/MM/dd") + "# and i_date <=#" + m_date.AddDays(1).ToString("yyyy/MM/dd") + "# order by i_date desc";
                IncFrm ifrm = new IncFrm(sql);
                ifrm.Show();
            }
        }

        private void getSum(string sql)
        {
            string sumIncSql = "select iif(sum(t.i_num_one) is null,0,sum(t.i_num_one)) as sumone,iif(sum(t.i_num_one) is null,0,sum(t.i_num_second)) as sumsecond from incpay t where t.i_iorp = 0 " + sql;
            string sumPaySql = "select iif(sum(t.i_num_one) is null,0,sum(t.i_num_one)) as sumone,iif(sum(t.i_num_one) is null,0,sum(t.i_num_second)) as sumsecond from incpay t where t.i_iorp = 1 " + sql;
            int incSumOne = 0;
            int incSumSecond = 0;
            int paySumOne = 0;
            int paySumSecond = 0;
            DataSet incDs = AccessHelper.ExecuteQuery(sumIncSql);
            foreach (DataRow dr in incDs.Tables[0].Rows)
            {
                incSumOne = Convert.ToInt32(dr[0].ToString());
                incSumSecond = Convert.ToInt32(dr[1].ToString());
            }

            DataSet payDs = AccessHelper.ExecuteQuery(sumPaySql);
            foreach (DataRow dr in payDs.Tables[0].Rows)
            {
                paySumOne = Convert.ToInt32(dr[0].ToString());
                paySumSecond = Convert.ToInt32(dr[1].ToString());
            }

            this.labSumInc.Text = "本月统计 收入总和：" + incSumOne.ToString() + "  贷入总和：" + incSumSecond.ToString();
            this.labSumPay.Text = "支出总和：" + paySumOne.ToString() + "  借出总和：" + paySumSecond.ToString();
            this.labSumTotal.Text = "收支结余：" + (incSumOne - paySumOne).ToString();

        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            ExcelUtil.saveDataGridView(this.dataGridViewIncMonth, "月统计");
        }

        private void IncMonthFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            HomeFrm.imf = null;
        }
    }
}
